package com.uzai.console.service.wwgroup.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.dto.GroupCreateTaskRecordQueryDTO;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.DeviceWxworkInfo;
import com.uzai.console.entity.GroupCreateTask;
import com.uzai.console.entity.GroupCreateTaskRecord;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.DeviceWxworkInfoMapper;
import com.uzai.console.mapper.GroupCreateTaskMapper;
import com.uzai.console.mapper.GroupCreateTaskRecordMapper;
import com.uzai.console.service.wwgroup.GroupCreateTaskRecordService;
import com.uzai.console.vo.GroupCreateTaskRecordVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 建群任务记录(GroupCreateTaskRecord)表服务实现类
 *
 * @author szl
 * @since 2024-04-23 16:29:34
 */
@Service
public class GroupCreateTaskRecordServiceImpl extends ServiceImpl<GroupCreateTaskRecordMapper, GroupCreateTaskRecord> implements GroupCreateTaskRecordService {

    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private GroupCreateTaskMapper groupCreateTaskMapper;

    @Override
    public Page<GroupCreateTaskRecordVO> search(GroupCreateTaskRecordQueryDTO query) {

        GroupCreateTask groupCreateTask = groupCreateTaskMapper.selectById(query.getTaskId());
        if (Objects.isNull(groupCreateTask)) {
            throw new BusinessException("数据错误");
        }
        boolean isWw = Objects.equals(groupCreateTask.getDeviceType(), 9);

        Page<GroupCreateTaskRecordVO> result = new Page<>(query.getCurrent(), query.getSize());

        Page<GroupCreateTaskRecord> page = new Page<>(query.getCurrent(), query.getSize());
        query.convert(page);
        LambdaQueryWrapper<GroupCreateTaskRecord> condition = Wrappers.lambdaQuery();
        condition.eq(GroupCreateTaskRecord::getMerId, query.getMerId());
        condition.eq(Objects.nonNull(query.getTaskId()), GroupCreateTaskRecord::getTaskId, query.getTaskId());
        condition.eq(Objects.nonNull(query.getDeviceUniqueId()), GroupCreateTaskRecord::getDeviceUniqueId, query.getDeviceUniqueId());
        page = this.getBaseMapper().selectPage(page, condition);
        List<GroupCreateTaskRecord> records = page.getRecords();
        if (ObjectUtils.isEmpty(records)) {
            return result;
        }
        List<GroupCreateTaskRecordVO> resultRecords = records.stream().map(item -> {
            GroupCreateTaskRecordVO vo = new GroupCreateTaskRecordVO();
            BeanUtils.copyProperties(item, vo);
            if (isWw) {
                DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoMapper.selectById(item.getDeviceUniqueId(), item.getMerId());
                if (Objects.nonNull(deviceWxworkInfo)) {
                    vo.setDeviceName(deviceWxworkInfo.getDeviceName());
                }
            } else {
                DeviceWechatInfo deviceWechatInfo = deviceWechatInfoMapper.selectById(item.getDeviceUniqueId(), item.getMerId());
                if (Objects.nonNull(deviceWechatInfo)) {
                    vo.setDeviceName(deviceWechatInfo.getDeviceName());
                }
            }
            vo.setGroupMembers(JSON.parseArray(item.getGroupMembers(), String.class));
            return vo;
        }).collect(Collectors.toList());
        //
        return result.setRecords(resultRecords).setTotal(page.getTotal());
    }

    @Override
    public Boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public void deleteByTaskId(Long merId, Long taskId) {
        this.getBaseMapper().deleteByTaskId(merId, taskId);
    }

    @Override
    public List<GroupCreateTaskRecord> listByTaskId(Long merId, Long taskId) {
        return this.getBaseMapper().selectListByTaskId(merId, taskId);
    }

    @Override
    public List<Long> listIdsByTaskId(Long merId, Long taskId) {
        return this.getBaseMapper().selectIdListByTaskId(merId, taskId);
    }

    @Override
    public List<GroupCreateTaskRecord> listByTaskId(Long merId, Long taskId, Long deviceUniqueId) {
        return this.getBaseMapper().selectListByTaskIdWithStatus0(merId, taskId, deviceUniqueId);
    }
}

